import {combineReducers, createStore, applyMiddleware, compose} from 'redux'
import {persistStore, persistReducer} from 'redux-persist';

import AsyncStorage from '@react-native-community/async-storage';
import {createLogger} from "redux-logger";

import thunk from 'redux-thunk'
import userReducer from './userReducer'

// 屏蔽flow误报警
declare var console: any;

const persistConfig = {
    key: 'root', // 对于数据 key 的定义
    storage: AsyncStorage,     // 选择的存储引擎
}
let reducers = {
    userReducer
}
// 对 reducers 的封装处理
const persistedReducer = persistReducer(persistConfig, combineReducers(reducers))

let loggerMiddleware = createLogger();

export default function configureStore() {
    const enhancers = compose(
        applyMiddleware(thunk, loggerMiddleware),
    );

    //处理后的 reducers 需要作为参数传递在 createStore 中
    const store = createStore(persistedReducer, enhancers)

    // 持久化 store
    let persistor = persistStore(store)

    return {store, persistor}
}
